Imports System.Xml
Imports System.Xml.Serialization
Imports ToDoList.Model.TaskQuery

Namespace Model
  ''' <summary>
  ''' class representing the todolist
  ''' </summary>
  <XmlType("TODOLIST")> _
Public Class Todolist
    Inherits TaskContainer

#Region "Repository functions"

    Public Enum ContextRelation
      Sibling
      Child
    End Enum

    ''' <summary>
    ''' Return the task with the given id
    ''' </summary>
    Public Function GetTask(ByVal taskId As Integer) As Task
      Return executeQuery(New SearchTaskWithId(taskId))
    End Function

    ''' <summary>
    ''' Find parent for given task
    ''' </summary>
    Public Function GetParentTask(ByVal task As Task) As Task
      Return executeQuery(New SearchParent(task))
    End Function

    ''' <summary>
    ''' Execute a query against our task collection
    ''' </summary>
    ''' <param name="query">the query to execute</param>
    Private Function executeQuery(ByVal query As AbstractTaskQuery) As Task
      For Each lTask As Task In mTasks
        lTask.Query(query)
        If query.Success Then
          Exit For
        End If
      Next
      Return query.Result
    End Function

    ''' <summary>
    ''' Create a new task
    ''' </summary>
    ''' <param name="contextTask">Context in which the task will be created</param>
    ''' <param name="relation">how the new task is related to the contexttask</param>
    ''' <returns>the new task</returns>
    Public Function CreateNewTask(ByVal contextTask As Task, ByVal relation As ContextRelation) As Task
      Dim lNewTask As New Model.Task
      Dim lParent As TaskContainer = Nothing

      'find parent of this task (=parent of the context task or nothing)
      If contextTask IsNot Nothing Then
        If relation = ContextRelation.Sibling Then
          lParent = GetParentTask(contextTask)
        Else
          lParent = contextTask
        End If
      End If
      If lParent Is Nothing Then
        'this is a root node, me is the container
        lParent = Me
      End If

      'give the task a unique id
      lNewTask.Id = Me.NextUniqueId
      NextUniqueId += 1

      'add task into the container
      lParent.Add(lNewTask)

      Return lNewTask
    End Function
#End Region

#Region "Serialized Properties"

    Private mTasks As Task() = New Task() {}
    <XmlElement("TASK")> _
    Public Property Tasks() As Task()
      Get
        Return mTasks
      End Get
      Set(ByVal value As Task())
        mTasks = value
      End Set
    End Property

    ''' <summary>
    ''' See description in base class. This property is 
    ''' for internal use in the base class (and does as such 
    ''' reflect bad design)
    ''' </summary>
    Public Overrides Property ChildTasks() As Task()
      Get
        Return mTasks
      End Get
      Set(ByVal value As Task())
        mTasks = value
      End Set
    End Property

    Private mFileFormat As String
    <XmlAttribute("FILEFORMAT")> _
    Public Property FileFormat() As String
      Get
        Return mFileFormat
      End Get
      Set(ByVal value As String)
        mFileFormat = value
      End Set
    End Property

    Private mProjectName As String = "Tasklist"
    <XmlAttribute("PROJECTNAME")> _
    Public Property ProjectName() As String
      Get
        Return mProjectName
      End Get
      Set(ByVal value As String)
        mProjectName = value
      End Set
    End Property

    Private mNextUniqueId As Integer
    <XmlAttribute("NEXTUNIQUEID")> _
    Public Property NextUniqueId() As Integer
      Get
        Return mNextUniqueId
      End Get
      Set(ByVal value As Integer)
        mNextUniqueId = value
      End Set
    End Property

    Private mEarliestDueDate As Double
    <XmlAttribute("EARLIESTDUEDATE")> _
    Public Property EarliestDueDate() As Double
      Get
        Return mEarliestDueDate
      End Get
      Set(ByVal value As Double)
        mEarliestDueDate = value
      End Set
    End Property

    Private mFileVersion As Integer
    <XmlAttribute("FILEVERSION")> _
    Public Property FileVersion() As Integer
      Get
        Return mFileVersion
      End Get
      Set(ByVal value As Integer)
        mFileVersion = value
      End Set
    End Property

    Private mLastModified As Date
    <XmlAttribute("LASTMODIFIED")> _
    Public Property LastModified() As Date
      Get
        Return mLastModified
      End Get
      Set(ByVal value As Date)
        mLastModified = value
      End Set
    End Property

#End Region

  End Class
End Namespace


